package sliding_window;

public class totalFruit {
    public int totalFruit(int[] fruits) {
        int[] hash = new int[100001];
        int ret = 0;
        for (int left = 0, flg = 0, right = 0; right < fruits.length; right++) {
            // 进窗口
            if (hash[fruits[right]] == 0) {
                ++flg;
            }
            hash[fruits[right]]++;
            while (flg > 2) {//判断
                hash[fruits[left]]--;//出窗口

                if(hash[fruits[left]] == 0) {
                    flg--;
                }
                left++;
            }
            ret = Math.max(right-left+1,ret);
        }
        return ret;

    }
}
